package logic.pruebas;

import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.JFrame;

import logic.blocks.Block;
import logic.characters.Buu;
import logic.characters.Crash;
import logic.characters.Donatello;
import logic.characters.Falcon;
import logic.characters.Grievous;
import logic.characters.Jigglypuff;
import logic.characters.Kirby;
import logic.characters.Link;
import logic.characters.Marco;
import logic.characters.Mario;
import logic.characters.Player;
import logic.characters.Samus;
import logic.characters.Snake;
import logic.characters.Urahara;
import logic.characters.Wario;
import logic.characters.Zabuza;
import logic.enemies.Enemy;
import logic.entities.Background;
import logic.utils.MapLoader;

/** Ventana principal del juego de prueba
 * @author Unai
 */
public class GameWindow extends JFrame {
	private static final long serialVersionUID = 4393904412863366001L;
	private static Dimension TAM_MINIMO_VENTANA = new Dimension( 800, 600 );
	private GamePanel panelJuego;
	private Player jugador;

	/** Construye una ventana
	 */
	public GameWindow() { 
		setMinimumSize( TAM_MINIMO_VENTANA );
		setResizable(false);	
		setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		addKeyListener(new TAdapter());
		setFocusable(true);

	}

	/** Devuelve el panel de juego de la ventana
	 * @return	Panel principal donde se visualiza el juego
	 */
	public GamePanel getPanelJuego() {
		return panelJuego;
	}

	/** Inicializa el panel de juego
	 * @param panel
	 */
	public void setPanelJuego(GamePanel panel) {
		panelJuego = panel;
	}

	/** Inicializa el jugador
	 * @param jugador
	 */
	public void setJugador (Player jugador){
		this.jugador=jugador;
	}

	/**
	 * @author Unai
	 * Clase para el reconocimiento del pulsado de teclas
	 */
	private class TAdapter extends KeyAdapter{
		public void keyReleased(KeyEvent e){
			if(jugador!=null) {
				jugador.keyReleased(e);
			}
		}
		public void keyPressed(KeyEvent e){
			if(jugador!=null) {
				jugador.keyPressed(e);
			}
		}
		
		
	}

	public static void main (String [] args) {
		//		Test t = new Test();
		//		t.run();
		
		GameWindow win = new GameWindow();
		win.pack();
		win.setLocationRelativeTo(null);
		win.setVisible(true);
		GamePanel panel = new GamePanel(300);
		panel.setLayout(null);
		win.add(panel);
		panel.setVisible(true);
		String character = "Link";
		int[] abilities = new int [2];
		abilities[0] = 1;
		abilities[1] = 2;
		Link player = new Link(150, 350, panel, "resources" + character + "/IR.gif", 11, 1, 1, 1, 1, 1, 1, 1, abilities);
		Background background = new Background(0, 0, panel, "resources/back1.gif", player);
//		int adjust = background.setYPos(600);
		win.setJugador(player);
		ArrayList<Block> blocks = new ArrayList<Block>();
		ArrayList<Enemy> enemies = new ArrayList<Enemy>();
		panel.setPlayer(player); 
		
		new MapLoader(blocks, enemies, panel, player, 1, 1);

//		try{
//			FileInputStream map = new FileInputStream(new File("maps/test.txt"));
//			DataInputStream mapInput = new DataInputStream(map);
//			BufferedReader mapReader = new BufferedReader(new InputStreamReader(mapInput));
//			String mapLine;
//			int x = 0;
//			int y = 0;
//			
//			while ((mapLine = mapReader.readLine()) != null)   {
//				char [] characters = new char [mapLine.length()];
//				mapLine.getChars(0, mapLine.length(), characters, 0);
//				for (int i = 0; i < mapLine.length(); i++) {
//					switch (characters [i]) {
//					case 'B':
//						blocks.add(new Block(x, y - adjust, panel, "resources/block.png", player));
//						x = x + 50;
//						break;
//					case 'E':
//						enemies.add(new Enemy(x, y, panel, "resources/goombaL.gif", 5));
//						x = x + 50;
//						break;
//					case 'P':
//						//Nothing
//						break; 
//					case '.':
//						x = x + 50;
//						break;
//					case 'N':
//						y = y + 50;
//						x = 0;
//						break;
//					case '8':
//						blocks.add(new Block(x, y - adjust, panel, "resources/8Block.png", player));
//						x = x + 50;
//						break;
//					case '4':
//						blocks.add(new Block(x, y - adjust, panel, "resources/4Block.png", player));
//						x = x + 50;
//						break;
//					}
//				}
//			}
//			mapInput.close();
//		}catch (Exception e){
//			System.err.println("Error: " + e.getMessage());
//		}
		
//		panel.add(background);
//		panel.add(player);
//		for (Block b : blocks) {
//			panel.add(b);
//		}
//		
//		for (Enemy e : enemies) {
//			panel.add(e);
//		}
		
		background.dibujar();
		
		for (Block b : blocks) {
			b.dibujar();
//			b.checkRelevant(player);
		}
		
//		for (Enemy e : enemies) {
//			e.checkRelevant(player);
//			e.dibujar();
//		}
		player.dibujar();
		panel.updateUI();
//		Runnable playerThread = new PlayerThread(player);
//		Thread pplayer = new Thread (playerThread);
//		pplayer.run();
		player.enemies = enemies;
		player.blocks = blocks;		
		//System.out.println(player.getClass().getSimpleName());
		//System.out.println("resources." + player.getClass().getSimpleName() + "/IR.gif");
//		System.out.println(player.collisionX);
		panel.timer.start();
		while (true) {
			player.update();
//			System.out.println(background.coorX);
			
			for (Block b : blocks) {
				//b.checkRelevant(player);
				//if (b.relevant) {
				b.update(background);
				b.impact(player);
				background.updateBackground(enemies);
				for (Enemy e : enemies) {
					e.update(player);
//											System.out.println(e.coorX);
//											System.out.println(e.coorY);
					
					//						e.checkRelevant(player, background);
//					System.out.println(e.relevant);
					//						System.out.println(e.coorX);
					//						System.out.println(e.coorY);
					//if (e.relevant) {
						b.impact(e);
						e.impact(player);
					//}
				}
				//}
			}
			//			for (Enemy e : enemies) {
			//			e.update(player, background);
			//			e.impactoJugador(player);
			//			background.updateBackground(e);
			//			}
//			background.updateBackground(null);
			panel.drawTime();
			panel.updateBars();
			panel.updateUI();
		}

	}
}


//http://www.thejavahub.net/index.php?topic=563.msg2175#msg2175

